<template>
    <el-form ref="form" label-width="80px" style="padding:20px" size="mini">
        <el-form-item label="用户姓名">
            <el-input v-model="name" placeholder="管理员真实姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
            <el-input v-model="phone" placeholder="管理员手机号"></el-input>
        </el-form-item>
        <el-form-item label="管理账号">
            <el-input v-model="username" placeholder="登录美管家的管理员账号"></el-input>
        </el-form-item>
        <el-form-item label="登录密码">
            <el-input type="password" placeholder="登录美管家的管理员密码" v-model="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
            <el-input type="password" placeholder="确认两次输入的密码一致" v-model="password1"></el-input>
        </el-form-item>
        <el-form-item label="描述信息">
            <el-input type="textarea" :rows="2" placeholder="请填写管理员描述信息！" v-model="remarks"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submit">保存</el-button>
            <el-button @click="close">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'addAdmin',
  data () {
    return {
      id: 0,
      name: '',
      phone: '',
      username: '',
      password: '',
      password1: '',
      remarks: ''
    }
  },
  methods: {
    async submit () {
      if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(this.name))) {
        C.error('用户名为2到4个汉字')
        return false
      } else if (!(/^1[345678]\d{9}$/.test(this.phone))) {
        C.error('手机号不正确')
        return false
      } else if (!(/^[a-zA-Z0-9]{6,}$/.test(this.username))) {
        C.error('帐号只能为字母和数字且不能少于6位')
        return false
      } else if (!this.id && /^\s*$/.test(this.password)) {
        C.error('请输入账户密码')
        return false
      } else if (this.password !== this.password1) {
        C.error('两次密码输入不一致')
        return false
      }

      let data = {
        id: this.id,
        name: this.name,
        phone: this.phone,
        username: this.username,
        password: this.password,
        remarks: this.remarks
      }

      let res = await C.ajax('/shop/rule/addshopadmin', data, 'post')
      if (res) {
        C.closeBrowserWindow(res)
      }
    },
    close () {
      C.closeBrowserWindow()
    }
  },
  mounted: function () {
    let row = C.getDatas()
    if (row && row.id) {
      this.id = row.id
      this.name = row.name
      this.phone = row.phone
      this.username = row.username
      this.remarks = row.remarks
    }
  }
}
</script>